package cn.com.note.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.Utils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import cn.com.note.R;
import cn.com.note.view.CustomLayoutView;

/**
 * Created by sunfulin on 2019/7/23.
 */

public class ToastUtils {

    private static final int COLOR_DEFAULT = 0xFEFFFFFF;
    private static final int COLOR_SUCCESS = 0xFF2BB600;
    private static final int COLOR_WARNING = 0xFFFFC100;
    private static final int COLOR_ERROR = 0xFFFF0000;
    private static final int COLOR_MESSAGE = 0xFFFFFFFF;

    @Retention(RetentionPolicy.SOURCE)
    public @interface Duration {
    }

    private static int Radios = 25;
    private static int margin = 15;
    private static int marginBottom = 15;
    private static int textGravity = Gravity.CENTER_HORIZONTAL;
    private static Context mContext;
    private static Builder builder;

    public static Builder builder(Context context) {
        if (builder == null) {
            builder = new Builder(context.getApplicationContext());
        }
        return builder;
    }

    public static class Builder {

        public Builder(@NonNull Context context) {
            ToastUtils.mContext = context;
        }

        public ToastUtils.Builder setMargin(int margin) {
            ToastUtils.margin = margin;
            return this;
        }

        public ToastUtils.Builder setRadios(int radios) {
            ToastUtils.Radios = radios;
            return this;
        }

        public ToastUtils.Builder setMarginBottom(int marginBottom) {
            ToastUtils.marginBottom = marginBottom;
            return this;
        }

        public ToastUtils.Builder setTextGravity(int marginBottom) {
            ToastUtils.textGravity = marginBottom;
            return this;
        }
    }

    public enum STATUS {
        DEFAULT,
        SUCCESS,
        ERROR,
        WARING
    }

    public static void makeText(@NonNull Context context,
                                @NonNull CharSequence text, @Duration int duration, STATUS status, int gravity) {
        Toast result = new Toast(context);

        LayoutInflater inflate = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        CustomLayoutView v = (CustomLayoutView) inflate.inflate(R.layout.toast_transient_notification, null);
        v.setBgColor(getColor(status), dp2px(Radios));
        int width = getScreenWidth() - dp2px(margin);
        TextView tv = (TextView) v.findViewById(R.id.message);
        tv.setGravity(textGravity);
        tv.setWidth(width);
        tv.setText(text);
        result.setView(v);
        result.setGravity(gravity, 0, marginBottom);
        result.setDuration(duration);
        result.show();
    }

    public static void showToast(@NonNull CharSequence text, int gravity) {
        showToast(text, gravity, false);
    }

    public static void showToast(@NonNull CharSequence text, int gravity, boolean isLong) {
        makeText(mContext, text, isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT, STATUS.DEFAULT, gravity);
    }

    public static void showToast(@NonNull CharSequence text) {
        showToast(text, Gravity.BOTTOM);
    }

    public static void showToast(@NonNull CharSequence text, boolean isLong) {
        showToast(text, Gravity.BOTTOM, isLong);
    }

    public static void showError(@NonNull CharSequence text, int gravity, boolean isLong) {
        makeText(mContext, text, isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT, STATUS.ERROR, gravity);
    }

    public static void showError(@NonNull CharSequence text, int gravity) {
        showError(text, gravity, false);
    }

    public static void showError(@NonNull CharSequence text, boolean isLong) {
        showError(text, Gravity.BOTTOM, isLong);
    }

    public static void showError(@NonNull CharSequence text) {
        showError(text, false);
    }

    public static void showSuccess(@NonNull CharSequence text, int gravity, boolean isLong) {
        makeText(mContext, text, isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT, STATUS.SUCCESS, gravity);
    }

    public static void showSuccess(@NonNull CharSequence text, int gravity) {
        showSuccess(text, gravity, false);
    }

    public static void showSuccess(@NonNull CharSequence text) {
        showSuccess(text, false);
    }

    public static void showSuccess(@NonNull CharSequence text, boolean isLong) {
        showSuccess(text, Gravity.BOTTOM, isLong);
    }


    public static void showWaring(@NonNull CharSequence text, int gravity, boolean isLong) {
        makeText(mContext, text, isLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT, STATUS.WARING, gravity);
    }

    public static void showWaring(@NonNull CharSequence text, boolean isLong) {
        showWaring(text, Gravity.BOTTOM, isLong);
    }

    public static void showWaring(@NonNull CharSequence text, int gravity) {
        showWaring(text, gravity, false);
    }

    public static void showWaring(@NonNull CharSequence text) {
        showWaring(text, false);
    }

    private static int getColor(STATUS status) {
        int color = Color.BLACK;
        switch (status) {
            case SUCCESS:
                color = COLOR_SUCCESS;
                break;
            case ERROR:
                color = COLOR_ERROR;
                break;
            case WARING:
                color = COLOR_WARNING;
                break;
            case DEFAULT:
                color = COLOR_DEFAULT;
                break;
            default:
                break;
        }
        return color;
    }

    public static int getScreenWidth() {
        WindowManager wm = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getSize(point);
        }
        return point.x;
    }

    public static int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
